<?php
namespace App\Models\ES;

use Elasticsearch\Common\Exceptions\Missing404Exception;
use Elasticsearch\ClientBuilder;

class Media extends ElasticBase
{
    const ROUTING = 'web';
    const MOVIE_INDEX = "movie_index";
    const MOVIE_TABLE = "movie_type";

    public $mid = '';
    public $title = '';
    public $description = '';

	public  function __construct()
	{
		parent::__construct();
	}

    protected function body()
    {
        return obj_to_array($this);
    }

    public function table()
    {
        return [
            'index'   => getenv("ELASTICSEARCH_PREFIX")."_".self::MOVIE_INDEX,
            'type'    => self::MOVIE_TABLE,
            'routing' => self::ROUTING,
//            'id'    => 0,
            'body'    => [],
        ];
    }

    
    public function create($arr = [ 'mid' => ['type' => 'integer' ],
                                    'title' => ['type' => 'text' ],
                                    'description' => ['type' => 'text' ],
                                    'cover'=> ['type'=>'text','index'=>false]])
    {
        parent::create($arr);
    }


    public function get_list($keyword = '', $size = 10,$from = 0)
    {
        if(!$keyword){
            return false;
        }
        $result = [];
        // should： 或查询 must：and 查询
        $params['query']['bool']['should'] = array(
            array('match' => array('description' => $keyword)),
            array('match' => array('title' => $keyword))
        );
        // 高亮查询
        $params['highlight'] = [
            'pre_tags' => ["<font color='#FF4855'>"],
            'post_tags' => ["</font>"],
            'fields' => [
                "title" => new \stdClass(),
                "description" => new \stdClass(),
            ]
        ];
        $result = parent::search($params,$size,$from);
        return $result;
    }
}